1 /*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.java2d.pisces;
27
28 import java.util.Arrays;
29
30 /**
31 * An object used to cache pre-rendered complex paths.
32 *
33 * @see PiscesRenderer#render
34 */
35 final class PiscesCache {
36
37 final int bboxX0, bboxY0, bboxX1, bboxY1;
38
39 // rowAARLE[i] holds the encoding of the pixel row with y = bboxY0+i.
40 // The format of each of the inner arrays is: rowAARLE[i][0,1] = (x0, n)
41 // where x0 is the first x in row i with nonzero alpha, and n is the
42 // number of RLE entries in this row. rowAARLE[i][j,j+1] for j>1 is
43 // (val,runlen)
44 final int[][] rowAARLE;
45
46 // RLE encodings are added in increasing y rows and then in increasing
47 // x inside those rows. Therefore, at any one time there is a well
48 // defined position (x,y) where a run length is about to be added (or
49 // the row terminated). x0,y0 is this (x,y)-(bboxX0,bboxY0). They
50 // are used to get indices into the current tile.
51 private int x0 = Integer.MIN_VALUE, y0 = Integer.MIN_VALUE;
52
53 // touchedTile[i][j] is the sum of all the alphas in the tile with
54 // y=i*TILE_SIZE+bboxY0 and x=j*TILE_SIZE+bboxX0.
55 private final int[][] touchedTile;
56
57 static final int TILE_SIZE_LG = 5;
58 static final int TILE_SIZE = 1 << TILE_SIZE_LG; // 32
59 private static final int INIT_ROW_SIZE = 8; // enough for 3 run lengths
60
61 PiscesCache(int minx, int miny, int maxx, int maxy) {
62 assert maxy >= miny && maxx >= minx;
63 bboxX0 = minx;
64 bboxY0 = miny;
65 bboxX1 = maxx + 1;
66 bboxY1 = maxy + 1;
67 // we could just leave the inner arrays as null and allocate them
68 // lazily (which would be beneficial for shapes with gaps), but we
69 // assume there won't be too many of those so we allocate everything
70 // up front (which is better for other cases)
71 rowAARLE = new int[bboxY1 - bboxY0 + 1][INIT_ROW_SIZE];
72 x0 = 0;
73 y0 = -1; // -1 makes the first assert in startRow succeed
74 // the ceiling of (maxy - miny + 1) / TILE_SIZE;
75 int nyTiles = (maxy - miny + TILE_SIZE) >> TILE_SIZE_LG;
76 int nxTiles = (maxx - minx + TILE_SIZE) >> TILE_SIZE_LG;
77
78 touchedTile = new int[nyTiles][nxTiles];
79 }
80
81 void addRLERun(int val, int runLen) {
82 if (runLen > 0) {
83 addTupleToRow(y0, val, runLen);
84 if (val != 0) {
85 // the x and y of the current row, minus bboxX0, bboxY0
86 int tx = x0 >> TILE_SIZE_LG;
87 int ty = y0 >> TILE_SIZE_LG;
88 int tx1 = (x0 + runLen - 1) >> TILE_SIZE_LG;
89 // while we forbid rows from starting before bboxx0, our users
90 // can still store rows that go beyond bboxx1 (although this
91 // shouldn't happen), so it's a good idea to check that i
92 // is not going out of bounds in touchedTile[ty]
93 if (tx1 >= touchedTile[ty].length) {
94 tx1 = touchedTile[ty].length - 1;
95 }
96 if (tx <= tx1) {
97 int nextTileXCoord = (tx + 1) << TILE_SIZE_LG;
98 if (nextTileXCoord > x0+runLen) {
99 touchedTile[ty][tx] += val * runLen;
100 } else {
101 touchedTile[ty][tx] += val * (nextTileXCoord - x0);
102 }
103 tx++;
104 }
105 // don't go all the way to tx1 - we need to handle the last
106 // tile as a special case (just like we did with the first
107 for (; tx < tx1; tx++) {
108 // try {
109 touchedTile[ty][tx] += (val << TILE_SIZE_LG);
110 // } catch (RuntimeException e) {
111 // System.out.println("x0, y0: " + x0 + ", " + y0);
112 // System.out.printf("tx, ty, tx1: %d, %d, %d %n", tx, ty, tx1);
113 // System.out.printf("bboxX/Y0/1: %d, %d, %d, %d %n",
114 // bboxX0, bboxY0, bboxX1, bboxY1);
115 // throw e;
116 // }
117 }
118 // they will be equal unless x0>>TILE_SIZE_LG == tx1
119 if (tx == tx1) {
120 int lastXCoord = Math.min(x0 + runLen, (tx + 1) << TILE_SIZE_LG);
121 int txXCoord = tx << TILE_SIZE_LG;
122 touchedTile[ty][tx] += val * (lastXCoord - txXCoord);
123 }
124 }
125 x0 += runLen;
126 }
127 }
128
129 void startRow(int y, int x) {
130 // rows are supposed to be added by increasing y.
131 assert y - bboxY0 > y0;
132 assert y <= bboxY1; // perhaps this should be < instead of <=
133
134 y0 = y - bboxY0;
135 // this should be a new, uninitialized row.
136 assert rowAARLE[y0][1] == 0;
137
138 x0 = x - bboxX0;
139 assert x0 >= 0 : "Input must not be to the left of bbox bounds";
140
141 // the way addTupleToRow is implemented it would work for this but it's
142 // not a good idea to use it because it is meant for adding
143 // RLE tuples, not the first tuple (which is special).
144 rowAARLE[y0][0] = x;
145 rowAARLE[y0][1] = 2;
146 }
147
148 int alphaSumInTile(int x, int y) {
149 x -= bboxX0;
150 y -= bboxY0;
151 return touchedTile[y>>TILE_SIZE_LG][x>>TILE_SIZE_LG];
152 }
153
154 int minTouched(int rowidx) {
155 return rowAARLE[rowidx][0];
156 }
157
158 int rowLength(int rowidx) {
159 return rowAARLE[rowidx][1];
160 }
161
162 private void addTupleToRow(int row, int a, int b) {
163 int end = rowAARLE[row][1];
164 rowAARLE[row] = Helpers.widenArray(rowAARLE[row], end, 2);
165 rowAARLE[row][end++] = a;
166 rowAARLE[row][end++] = b;
167 rowAARLE[row][1] = end;
168 }
169
170 @Override
171 public String toString() {
172 String ret = "bbox = ["+
173 bboxX0+", "+bboxY0+" => "+
174 bboxX1+", "+bboxY1+"]\n";
175 for (int[] row : rowAARLE) {
176 if (row != null) {
177 ret += ("minTouchedX=" + row[0] +
178 "\tRLE Entries: " + Arrays.toString(
179 Arrays.copyOfRange(row, 2, row[1])) + "\n");
180 } else {
181 ret += "[]\n";
182 }
183 }
184 return ret;
185 }
186 }